﻿@{
    ViewData["Title"] = "任务调度列表";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label class="layui-form-label">作业编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="jobId" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">描述信息</label>
                    <div class="layui-input-inline">
                        <input type="text" name="description" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn" lay-submit lay-filter="btnsearch">
                            <i class="layui-icon layui-icon-search"></i> 查询
                        </button>
                    </div>
                </div>
            </form>

            <table id="lsttable" lay-filter="lsttable"></table>
            <script type="text/html" id="toolbar">
                <button class="layui-btn layui-btn-sm" lay-event="add" title="添加作业">
                    <i class="layui-icon layui-icon-addition"></i>添加
                </button>
                <div class="layui-btn-group">
                    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="startAllJob" title="启动所有作业">
                        <i class="layui-icon layui-icon-play"></i>
                    </button>
                     <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="pauseAllJob" title="暂停所有作业">
                        <i class="layui-icon layui-icon-pause"></i>
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="cancelSleep" title="强制唤醒作业调度器">
                        <i class="layui-icon layui-icon-time"></i>
                    </button>
                     <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="persistAll" title="强制触发所有作业持久化">
                        <i class="layui-icon layui-icon-refresh"></i>
                    </button>
                </div>
                 <button class="layui-btn layui-btn-sm" lay-event="dashboard" title="任务看板">
                    <i class="layui-icon layui-icon-chart-screen"></i>任务看板
                </button>
            </script>
            <script type="text/html" id="bar">
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="addTrigger">添加触发器</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="TriggerRecord">执行记录</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="del">删除</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="more">
                  更多
                  <i class="layui-icon layui-icon-down"></i>
                </a>
            </script>
            <script type="text/html" id="childBar">
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="startTrigger" title="启动触发器">
                    <i class="layui-icon layui-icon-play"></i>
                </a>
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="pauseTrigger" title="暂停触发器">
                    <i class="layui-icon layui-icon-pause"></i>
                </a>
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="editTrigger" title="编辑触发器">
                    <i class="layui-icon layui-icon-edit"></i>
                </a>
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="delTrigger" title="删除触发器">
                  <i class="layui-icon layui-icon-close"></i>
                </a>
            </script>
        </div>
    </div>
</div>

@section scripts {
    <script type="text/javascript">
        var app = {};
        layui.use(['tools', 'toast', 'http', 'soulTable'], function () {
            var table = layui.table, soulTable = layui.soulTable,
                $ = layui.$,
                form = layui.form, dropdown = layui.dropdown,
                http = layui.http,
                toast = layui.toast,
                tools = layui.tools;

            app.init = function () {
                //
                form.on('submit(btnsearch)', function (data) {
                    console.log(data.field)
                    table.reload('lsttable', {
                        where: data.field
                    })
                    return false;
                });
                //
                var status = ['积压', '就绪', '正在运行', '暂停', '阻塞', '由失败进入就绪', '归档', '崩溃', '超限', '无触发时间', '未启动', '未知作业触发器', '未知作业处理程序']
                let cols =
                    [[
                        {
                            title: '#', width: 50, children: [
                                {
                                    title: '触发器任务'
                                    , height: 200
                                    , limit: 1000000
                                    , data: function (d) {
                                        return d.jobTriggers;
                                    }
                                    , page: false
                                    , cols: [[
                                        { type: 'numbers' },
                                        { field: 'triggerId', title: '触发器编号', width: 120 },
                                        { field: 'triggerType', title: '类型', width: 220 },
                                        { field: 'args', title: '参数', width: 200 },
                                        { field: 'description', title: '描述',minWidth:160 },
                                        {
                                            field: 'status', title: '状态', width: 100, templet: function (d) {
                                                return '<span class="layui-badge layui-bg-blue">' + status[d.status] + '</span>';
                                            }
                                        },
                                        { field: 'startTime', title: '起始时间', width: 165 },
                                        { field: 'endTime', title: '结束时间', width: 165 },
                                        { field: 'lastRunTime', title: '最近运行时间', width: 165 },
                                        { field: 'nextRunTime', title: '下一次运行时间', width: 165 },
                                        { field: 'numberOfRuns', title: '触发次数', width: 120 },
                                        { field: 'maxNumberOfRuns', title: '最大触发次数', width: 120 },
                                        { field: 'numberOfErrors', title: '出错次数', width: 120 },
                                        { field: 'maxNumberOfErrors', title: '最大出错次数', width: 120 },
                                        { field: 'numRetries', title: '重试次数', width: 120 },
                                        { field: 'retryTimeout', title: '重试间隔ms', width: 120 },
                                        {
                                            title: '是否立即启动', field: 'concurrent', align: 'center', width: 120, templet: function (d) {
                                                return d.startNow ? '是' : '否';
                                            }
                                        },
                                        { fixed: 'right', title: '操作', toolbar: '#childBar', align: 'right', width: 200 }
                                    ]]
                                    , toolEvent: function (obj, pobj) {
                                        var data = obj.data;
                                        switch (obj.event) {
                                            case 'startTrigger':
                                                app.startTrigger(data);
                                                break;
                                            case 'pauseTrigger':
                                                app.pauseTrigger(data);
                                                break;
                                            case 'editTrigger':
                                                app.addTrigger(data);
                                                break;
                                            case 'delTrigger':
                                                app.delTrigger(data);
                                                break;
                                        };
                                    }
                                    , done: function () {
                                        soulTable.render(this);
                                    }
                                }
                            ]
                        },
                        { title: '序号', type: "numbers" },
                        { title: '作业编号', field: 'jobId', width: 160, templet: function (d) { return d.jobDetail.jobId } },
                        { title: '组名称', field: 'groupName', width: 120, templet: function (d) { return d.jobDetail.groupName } },
                        { title: '描述', field: 'description', templet: function (d) { return d.jobDetail.description } },
                        { title: '类型', field: 'jobType', width: 220, templet: function (d) { return d.jobDetail.jobType } },
                        {
                            title: '执行方式', field: 'concurrent', align: 'center', width: 100, templet: function (d) {
                                return d.jobDetail.concurrent ? '并行' : '串行';
                            }
                        },
                        {
                            title: '作业类型', field: 'createType', align: 'center', width: 100, templet: function (d) {
                                if (d.jobDetail.createType === 0) {
                                    return '<span class="layui-badge layui-bg-blue">内置</span>';
                                }
                                else if (d.jobDetail.createType === 1) {
                                    return '<span class="layui-badge layui-bg-green">脚本</span>';
                                }
                                else {
                                    return '<span class="layui-badge layui-bg-green">http</span>';
                                }
                            }
                        },
                        { title: '更新时间', field: 'updatedTime', width: 160, templet: function (d) { return d.jobDetail.updatedTime } },
                        { fixed: 'right', title: '操作', toolbar: '#bar', align: 'right', width: 350 }
                    ]];
                //
                tools.PageTable({
                    url: '/api/sysJob/pageJobDetail',
                    cols,
                    done: function () {
                        soulTable.render(this)
                    }
                });
                table.on('toolbar(lsttable)', function (obj) {
                    var id = obj.config.id;
                    var checkStatus = table.checkStatus(id);
                    switch (obj.event) {
                        case 'add':
                            app.addJob();
                            break;
                        case 'startAllJob':
                            app.allJob('/api/sysJob/startAllJob', '启动所有作业');
                            break;
                        case 'pauseAllJob':
                            app.allJob('/api/sysJob/pauseAllJob', '暂停所有作业');
                            break;
                        case 'cancelSleep':
                            app.allJob('/api/sysJob/cancelSleep', '强制唤醒作业调度器');
                            break;
                        case 'persistAll':
                            app.allJob('/api/sysJob/persistAll', '强制触发所有作业持久化');
                            break;
                        case 'dashboard':
                            parent.changePage({
                                id: 'dashboard',
                                title: '任务看板',
                                url: '/schedule',
                                close: true
                            })
                            break;
                    };
                });
                table.on('tool(lsttable)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'edit':
                            app.addJob(data.jobDetail);
                            break;
                        case 'del':
                            app.delJob(data.jobDetail.jobId);
                            break;
                        case 'addTrigger':
                            app.addTrigger({ jobId: data.jobDetail.jobId, retryTimeout: 10000,args:[3000] });
                            break;
                        case 'TriggerRecord':
                            app.triggerRecord(data.jobDetail.jobId);
                            break;
                        case 'more':
                            app.more(this, data.jobDetail);
                            break;
                    };
                });
            }();
            app.more = function (obj, data) {
                dropdown.render({
                    elem: obj, // 触发事件的 DOM 对象
                    show: true, // 外部事件触发即显示
                    data: [{
                        title: '执行作业',
                        id: 'run'
                    }, {
                        title: '启动作业',
                        id: 'start'
                    }, {
                        title: '暂停作业',
                        id: 'pause'
                    }, {
                        title: '取消作业',
                        id: 'cancel'
                    }],
                    click: function (menudata) {
                        switch (menudata.id) {
                            case 'run':
                                app.job('/api/sysJob/runJob', data, menudata.title);
                                break;
                            case 'start':
                                app.job('/api/sysJob/startJob', data, menudata.title);
                                break;
                            case 'pause':
                                app.job('/api/sysJob/pauseJob', data, menudata.title);
                                break;
                            case 'cancel':
                                app.job('/api/sysJob/cancelJob', data, menudata.title);
                                break;
                        };
                    }
                })
            }
            //
            app.job = function (url, data, title) {
                http.post(url, { jobId: data.jobId }).then((res) => {
                    if (res.code == 200) {
                        toast.success({ message: title + '成功！' });
                    }
                    else {
                        layer.alert('失败，错误信息' + res.message, { icon: 2 });
                    }
                })
            }
            app.allJob = function (url, title) {
                http.post(url).then((res) => {
                    if (res.code == 200) {
                        toast.success({ message: title + '成功！' });
                    }
                    else {
                        layer.alert('失败，错误信息' + res.message, { icon: 2 });
                    }
                })
            }
            app.addJob = function (data) {
                tools.open({
                    area: ['700px', '100%'],
                    content: './jobAdd',
                    title: '作业管理',
                    success: function (layero, index, that) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.app.data = data
                    },
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.app.save();
                    }
                });
            }
            app.delJob = function (id) {
                var index = layer.confirm('您确定要删除当前任务吗？', function () {
                    http.post('/api/sysJob/deleteJobDetail', { jobId: id }).then((res) => {
                        if (res.code == 200) {
                            layer.close(index);
                            table.reload('lsttable');
                            toast.success({ message: '删除成功！' });
                        }
                        else {
                            layer.alert('删除失败，错误信息' + res.message, { icon: 2 });
                        }
                    })
                });
            }
            app.startTrigger = function (data) {
                http.post('/api/sysJob/startTrigger', { jobId: data.jobId, triggerId: data.triggerId }).then((res) => {
                    if (res.code == 200) {
                        toast.success({ message: '启动成功！' });
                    }
                    else {
                        layer.alert('启动失败，错误信息' + res.message, { icon: 2 });
                    }
                })
            }
            app.pauseTrigger = function (data) {
                http.post('/api/sysJob/pauseTrigger', { jobId: data.jobId, triggerId: data.triggerId }).then((res) => {
                    if (res.code == 200) {
                        toast.success({ message: '暂停成功！' });
                    }
                    else {
                        layer.alert('暂停失败，错误信息' + res.message, { icon: 2 });
                    }
                })
            }

            app.triggerRecord = function (jobId) {
                console.log(jobId)
                tools.open({
                    area: ['800px', '100%'],
                    content: './TriggerRecord?jobId=' + jobId,
                    title: '执行记录',
                    btn: ['关闭']
                });
            }
            app.addTrigger = function (data) {
                tools.open({
                    area: ['800px', '100%'],
                    content: './triggerAdd',
                    title: '作业管理',
                    success: function (layero, index, that) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.app.data = data
                    },
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.app.save();
                    }
                });
            }
            app.delTrigger = function (data) {
                var index = layer.confirm('您确定要删除当前任务吗？', function () {
                    http.post('/api/sysJob/deleteJobTrigger', { jobId: data.jobId, triggerId: data.triggerId }).then((res) => {
                        if (res.code == 200) {
                            layer.close(index);
                            table.reload('lsttable');
                            toast.success({ message: '删除成功！' });
                        }
                        else {
                            layer.alert('删除失败，错误信息' + res.message, { icon: 2 });
                        }
                    })
                });
            }

        })
    </script>
}
